# adoption from chapter 05 generate data.r
path_data=normalizePath('../../2021-2022(2)')
class_data_files = list.files(path_data)

library(readxl)
library(haven)
library(expss)
class_id = 2
xls_file = normalizePath(file.path(path_data,class_data_files[class_id]))
df_class_data = read_excel(xls_file,col_names = c('id','student_id','name'))

generate_data = function(seed_num,student_name){
  set.seed(seed_num)
  output_folder = file.path(getwd(),'data','chapter06',paste(seed_num,student_name,sep="_"))
  dir.create(file.path(getwd(),'data','chapter06'))
  dir.create(output_folder)
  gs = factor(c(1,2),labels=c('阴性','阳性'))
  assay_pos = c(round(runif(1,min=10,max=20)),round(runif(1,min=50,max=80)))
  assay_neg = c(round(runif(1,min=45,max=60)),round(runif(1,min=25,max=35)))
  df = data.frame(gs=gs,assay_pos=assay_pos,assay_neg=assay_neg)
  df=apply_labels(df,
                  gs="金标准",assay_pos="比对试剂阳性",assay_neg="比对试剂阴性")
  write_sav(df,file.path(output_folder,'9_3_6.sav'))
  return(df)
}

for (row in 1:nrow(df_class_data)){
  student_id =as.numeric(df_class_data[row,'student_id']) %% 1000000
  name=df_class_data[row,'name']
  generate_data(student_id,name)
}
